package com.yuqing.sentimentapp.mine;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuqing.sentimentapp.base.BaseObserver;
import com.linkface.baselibrary.http.RetrofitClient;
import com.yuqing.sentimentapp.BuildConfig;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.api.ApiService;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.interfaces.ScheduleListener;
import com.yuqing.sentimentapp.mine.bean.MinePlatformInfoBean;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.utils.ToastUtil;
import com.yuqing.sentimentapp.utils.UpdateAppManager;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class YqInfoAboutActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_platform_desc)
    TextView mTvPlayformDesc;
    @BindView(R.id.tv_app_version)
    TextView mTvAppVersion;
    @BindView(R.id.btn_check_update)
    Button mBtnCheckUpdate;

    @Override
    public int getLayoutId() {
        return R.layout.activity_yq_info_about;
    }

    @Override
    public void initPresenter() {
        Observable<MinePlatformInfoBean> observable = RetrofitClient.getInstance(this, null, ModuleManager.getInstance().getHeaderMap()).create(ApiService.class).getPlatformInfo();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MinePlatformInfoBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(MinePlatformInfoBean minePlatformInfoBean) {
                        mTvPlayformDesc.setText(minePlatformInfoBean.getMessage());
                    }

                    @Override
                    protected void onFailure(String message) {
                        ToastUtil.showShort(message);
                    }
                });
    }

    @Override
    public void initView() {
        mTvTitle.setText("关于平台");
        mTvAppVersion.setText("v." + BuildConfig.VERSION_NAME);
    }

    @OnClick({R.id.btn_check_update, R.id.iv_back, R.id.tv_title})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_check_update:
                checkUpdate();
                break;
        }
    }

    private void checkUpdate() {
        if (UpdateAppManager.isUpdate()) {
            if (!UpdateAppManager.isDownLoad()) {
                UpdateAppManager.getUpdateAppManager().showDialog(YqInfoAboutActivity.this, true);
            } else {
                ToastUtil.showShort(getResources().getString(R.string.isDownLoad));
            }
        } else {
            UpdateAppManager.getUpdateAppManager().init();
            UpdateAppManager.getUpdateAppManager().onlineCheckAppUpdate(new ScheduleListener() {
                @Override
                public void progress(int progress) {

                }

                @Override
                public void start() {
                }

                @Override
                public void end(boolean success) {
                    if (UpdateAppManager.isUpdate()) {
                        if (!UpdateAppManager.isDownLoad()) {
                            UpdateAppManager.getUpdateAppManager().showDialog(YqInfoAboutActivity.this,
                                    true);
                        } else {
                            ToastUtil.showShort(getResources().getString(R.string.isDownLoad));
                        }
                    } else {
                        ToastUtil.showShort(YqInfoAboutActivity.this.getString(R.string.isUpdate));
                    }
                }
            });
        }
    }
}
